package com.meirimoney.business;

import android.content.Context;

import com.meirimoney.sdk.models.AccessToken;
import com.meirimoney.utilities.Device;
import com.meirimoney.utilities.Utility;

public class User
{

	/**
	 * User 的构造函数
	 * 
	 * @param context
	 *            当前执行的上下文
	 */
	private User(Context context)
	{
		this.context = context;
		this.setUserId();
		this.token = UserInformation.getInstance().getAccessToken(this.context);
		this.integralBalance = UserInformation.getInstance()
				.getIntegralBalance(this.context);
		this.loginStatus = false;
	}
	
	/**
	 * 设置用户Id
	 * 利用设备Id进行Md5加密
	 */
	private void setUserId()
	{
		String	deviceId=Device.getDeviceId(context);
		if(!deviceId.equals(""))
		{
			deviceId+="android";
		}
		this.userId = Utility.calculateMd5(deviceId);
	}

	/**
	 * 返回User对象
	 * 
	 * @return
	 */
	public static User get(Context context)
	{
		if (null == User.instance)
		{
			User.instance = new User(context);
		}
		return User.instance;
	}

	/***
	 * 获取用户Id
	 * 
	 * @return 设备Id的md5加密
	 */
	public String getUserId()
	{
		return this.userId;
	}

	/**
	 * 获取当前登录用户的Token信息
	 */
	public AccessToken getToken()
	{
		return this.token;
	}

	public void setToken(AccessToken accessToken)
	{
		UserInformation.getInstance()
				.saveAccessToken(this.context, accessToken);
		this.token = accessToken;
	}

	/***
	 * 获取积分余额
	 * 
	 * @return 返回积分余额
	 */
	public int getIntegralBalance()
	{
		return this.integralBalance;
	}

	/***
	 * 设置积分余额
	 * 
	 * @return 设置积分余额
	 */
	public void setIntegralBalance(int integralBalance)
	{
		UserInformation.getInstance().saveIntegralBalance(context,
				integralBalance);
		this.integralBalance = integralBalance;
	}
	
	/**
	 * 获取当前的登录状态
	 * @return
	 */
	public boolean getLoginStatus()
	{
		return this.loginStatus;
	}

	/**
	 * 设置当前的登录状态
	 * @param status
	 */
	public void setLoginStatus(boolean status)
	{
		this.loginStatus = status;
	}

	/**
	 * 静态字段（获取User对象）
	 */
	private static User instance;

	/***
	 * 当前的上下文信息
	 */
	private Context context;

	/***
	 * 用户标识
	 */
	private String userId;

	/**
	 * 用户的Token信息
	 */
	private AccessToken token;

	/**
	 * 用户的积分余额
	 */
	private Integer integralBalance;

	/**
	 * 当前的登录状态
	 */
	private boolean loginStatus;
}
